package com.example.nkj.radiobutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class radioButton extends AppCompatActivity {

    private RadioGroup baud;
    private RadioButton baud_9600;
    private RadioButton baud_19200;
    private RadioButton baud_115200;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        //通过控件的ID来得到代表控件的对象
        baud = (RadioGroup) findViewById(R.id.baud);
        baud_9600 = (RadioButton) findViewById(R.id.baud_9600);
        baud_19200 = (RadioButton) findViewById(R.id.baud_19200);
        baud_115200 = (RadioButton) findViewById(R.id.baud_115200);
        //默认第一个选中
        baud_9600.setChecked(true);
        //baud.check(0);

        //调用setOnCheckedChangeListener来对RadioGroup进行监听的代码
        baud.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                if(checkedId == baud_9600.getId())
                {
                    Toast.makeText(radioButton.this,"baud is 9600" , Toast.LENGTH_SHORT).show();
                }
                else if(checkedId == baud_19200.getId())
                {
                    Toast.makeText(radioButton.this,"baud is 19200" , Toast.LENGTH_SHORT).show();
                }
                else if(checkedId == baud_115200.getId())
                {
                    Toast.makeText(radioButton.this,"baud is 115200" , Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
